﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using TabStripLib.Win;

namespace WSBackup
{
    public partial class FormOptions : Form
    {
        private readonly WSSettingsXml settings;
        //private List<StringValue> extensions;
        private BindingSource extensions = new BindingSource();

        public FormOptions(WSSettingsXml settings)
        {
            InitializeComponent();

            this.settings = settings;
            tbComparer.Text = settings.Comparer;

            foreach (FileSelectionMethod fsm in Enum.GetValues(typeof(FileSelectionMethod)))
            {
                cmbFileSelectionMethod.Items.Add(fsm);
            }

            foreach (ExcludeType et in Enum.GetValues(typeof(ExcludeType)))
            {
                cmbExcludeType.Items.Add(et);
            }

            if (settings.Paths.Count > 0)
            {
                foreach (var path in settings.Paths)
                {
                    var p = new TabStripItem(path.Name) { Tag = path };
                    tabStrip1.AddTab(p);
                    if (path.Name == settings.LastArchive)
                        tabStrip1.SelectedItem = p;
                }
            }
            else
            {
                AddNewPath(null);
            }

        }

        private void btComparerBrowse_Click(object sender, EventArgs e)
        {
            ofdComparer.FileName = tbComparer.Text;
            if (ofdComparer.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
            {
                tbComparer.Text = ofdComparer.FileName;
            }
        }

        private void btOK_Click(object sender, EventArgs e)
        {
            if (!File.Exists(tbComparer.Text))
            {
                if (MessageBox.Show("Comparer doesn't exists. Even though close window?", "Really", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                {
                    this.DialogResult = DialogResult.None;
                }
            }
            else
            {
                settings.Comparer = tbComparer.Text;
            }

            if (string.IsNullOrEmpty(this.tbName.Text))
            {
                    MessageBox.Show("Name is mandatory.");
                    this.DialogResult = DialogResult.None;
                return;

            }

            if (this.tbSrcPath.Text != string.Empty || this.tbSrcPath.Text != string.Empty)
            {

                if (!Directory.Exists(this.tbSrcPath.Text))
                {
                    MessageBox.Show("Source folder does not exists.");
                    this.DialogResult = DialogResult.None;
                }

                if (!Directory.Exists(this.tbDstPath.Text))
                {
                    MessageBox.Show("Destination folder does not exists.");
                    this.DialogResult = DialogResult.None;
                }
            }

            this.SaveOptions((WSArchiveConfig)this.tabStrip1.SelectedItem.Tag);

            settings.LastArchive = this.tbName.Text.Trim();

        }


        private void tabStrip1_TabStripSelectionChanged(object sender, TabStripItemSelectionChangeEventArgs e)
        {
            if (e.ItemOld != null)
            {
                this.SaveOptions((WSArchiveConfig)e.ItemOld.Tag);
            }

            if (e.ItemNew != null)
            {
                this.LoadOptions((WSArchiveConfig)e.ItemNew.Tag);
            }
        }

        private void tabStrip1_TabStripItemAdding(object sender, TabStripItemAddingEventArgs e)
        {
            AddNewPath(e);
        }

        private void textBox1_Validating(object sender, CancelEventArgs e)
        {
            var path = (WSArchiveConfig)tabStrip1.SelectedItem.Tag;
            tabStrip1.SelectedItem.Caption = path.Name = tbName.Text;
        }

        private void AddNewPath(TabStripItemAddingEventArgs e)
        {
            var path = new WSArchiveConfig { Name = "New path" };
            var tabStripItem = new TabStripItem("New path") { Tag = path };

            if (e != null)
                e.Item = tabStripItem;
            else
                tabStrip1.AddTab(tabStripItem);

            settings.Paths.Add(path);
        }

        private void tabStrip1_TabStripItemClosing(object sender, TabStripItemClosingEventArgs e)
        {
            if (MessageBox.Show("Naozaj chcete odstrániť parametre?", "Otazka", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
            {
                e.Cancel = true;
                return;
            }

            settings.Paths.Remove((WSArchiveConfig)e.Item.Tag);
        }

        private bool SaveOptions(WSArchiveConfig oldArchiveConfig)
        {
            if (oldArchiveConfig == null)
            {
                return false;
            }
            oldArchiveConfig.Name = this.tbName.Text.Trim();
            oldArchiveConfig.SrcPath = this.tbSrcPath.Text;
            oldArchiveConfig.DestPath = this.tbDstPath.Text;
            oldArchiveConfig.FileSelectionMethod = (FileSelectionMethod)this.cmbFileSelectionMethod.SelectedItem;
            oldArchiveConfig.ExcludeType = (ExcludeType)this.cmbExcludeType.SelectedItem;
            oldArchiveConfig.Extensions = SV_TO_S(extensions);
            return true;
        }

        private void LoadOptions(WSArchiveConfig newArchiveConfig)
        {
            if (newArchiveConfig == null)
            {
                return;
            }
            tbName.Text = newArchiveConfig.Name;
            tbSrcPath.Text = newArchiveConfig.SrcPath;
            tbDstPath.Text = newArchiveConfig.DestPath;
            cmbFileSelectionMethod.SelectedItem = newArchiveConfig.FileSelectionMethod;
            cmbExcludeType.SelectedItem = newArchiveConfig.ExcludeType;
            extensions.DataSource = S_TO_SV(newArchiveConfig.Extensions);
            dgPripony.DataSource = extensions;
        }

        private List<StringValue> S_TO_SV(List<String> list)
        {
            if (list == null)
                return null;

            var rv = new List<StringValue>(list.Count);
            foreach (var s in list)
            {
                rv.Add(new StringValue(s));
            }

            return rv;
        }

        private List<String> SV_TO_S(BindingSource src)
        {
            List<StringValue> list = (List<StringValue>)src.DataSource;
            if (list == null)
                return null;

            var rv = new List<String>(list.Count);
            foreach (var s in list)
            {
                if (!string.IsNullOrWhiteSpace(s.Value))
                    rv.Add(s.Value);
            }
            return rv;
        }

        private void btAdd_Click(object sender, EventArgs e)
        {
            extensions.Add(new StringValue(""));
            //dgPripony.DataSource = null;
            //dgPripony.DataSource = extensions;
            dgPripony.CurrentCell = dgPripony.Rows[dgPripony.Rows.Count - 1].Cells[0];
            dgPripony.BeginEdit(true);
        }

        private void btDelete_Click(object sender, EventArgs e)
        {
            if (dgPripony.CurrentCell != null)
            {
                var i = dgPripony.CurrentCell.RowIndex;
                extensions.RemoveAt(i);
                dgPripony.DataSource = null;
                dgPripony.DataSource = extensions;
                if (extensions.Count > 0)
                {
                    dgPripony.CurrentCell = dgPripony.Rows[extensions.Count >= i ? i - 1 : i].Cells[0];
                }
            }
        }

    }

    public class StringValue
    {
        public StringValue(string s)
        {
            Value = s;
        }

        public string Value { get; set; }
    }

}
